﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Noebe;
using Pixysoft.Framework.XSchEmA.Entity;
using Pixysoft.Framework.XSchEmA;
using Pixysoft.IO;


namespace Pixysoft.Framework.Security
{
    class TokenHelper
    {
        public const string default_username = "HELLOWORLD";

        public const string default_password = "FC5E038D38A57032085441E7FE7010B0";//md5(helloworld)

        public const string default_dbname = "helloworld.db";

        public const int default_tokendb_TimeOut = 5;//min

        public const int default_tokencache_Timeout = 300;//sec 必须小于数据库token的时间

        public const string default_anonymous = "ANONYMOUS";//min

        public const string default_reloadfilename = "pixysoft.framework.security.user.config";

        public const string default_schemafilename = "pixysoft.framework.security.token.schema.config";


        private static volatile TokenHelper instance;

        private static object syncRoot = new Object();

        public static TokenHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new TokenHelper();
                        }
                    }
                }
                return instance;
            }
        }


        private TokenHelper()
        {
            if (!Initializes())
            {
                Deploy();

                NoebeManager.Instances[StringHelper.global_framework_id].Reloads(GetConfiguration());
            }
        }

        private bool Initializes()
        {
            if (!NoebeManager.Instances[StringHelper.global_framework_id].Reloads(
                 TokenHelper.GetConfiguration()))
                return false;

            //09-11-21 必须添加对表数量的判断

            if (NoebeManager.Instances[StringHelper.global_framework_id].GetEntity().Tables.Count == 0)
                return false;

            return true;
        }

        private void Deploy()
        {
            DatabaseSchema schema = ImbeddedReader.Instance.Import<DatabaseSchema>(this,
                TokenHelper.default_schemafilename);

            schema.Build();

            XSchemaManager.SQLiteSchema.Schema.TransformToSpecific(schema);

            XSchemaManager.SQLiteSchema.Schema.Commit(
                NoebeManager.Instances[StringHelper.global_framework_id].NoebeCommand,
                schema);

            NoebeManager.Instances[StringHelper.global_framework_id].
                Reloads(TokenHelper.GetConfiguration());
        }


        public static string GeneratePrimaryKey()
        {
            return Pixysoft.Tools.GlobalTimer.Instance.GetUniqueStringSequence();
        }

        public static DateTime DateTimeNow
        {
            get
            {
                return Pixysoft.Tools.GlobalTimer.Instance.GetGlobalTime();
            }
        }

        public static NoebeConfiguration GetConfiguration()
        {
            NoebeConfiguration config = new NoebeConfiguration();
            config.DatabaseType = Pixysoft.Framework.XSchEmA.DatabaseType.SQLite;
            config.Filename = AppDomain.CurrentDomain.BaseDirectory + @"\" + TokenHelper.default_dbname;

            return config;
        }


        public INoebeManager GetNoebeManager()
        {
            return NoebeManager.Instances[StringHelper.global_framework_id];
        }
    }

    public enum TokenStatus
    {
        Available,
        Frozen,
    }
}
